﻿/*
 * 文件名称：
 * 程序员：yylu
 * 创建时间：2009/04/29
 * 
 * 功能说明：栏目管理
 * 
 * 修改人员：
 * 
 * 修改日期：
 * 
 * 修改记录：1) 修改一些BUG,增加级联删除功能 yhe 2009-09-11
 */

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Whir.Common.WebHelper;

public partial class Whir_System_Module_Role_AddColumn : System.Web.UI.Page
{
    #region 属性
    /// <summary>
    /// 当前页面操作
    /// </summary>
    protected string Action = "";
    /// <summary>
    /// 当前栏目编号
    /// </summary>
    protected int ColumnID = 0;
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        RoleHelper.IsLogin();    //登录判断
        Action = NetRequest.GetQueryString("Action");
        ColumnID = NetRequest.GetQueryInt("ColumnID");

        if (Action == "Modify")
        {
            btnUpdate.Text = "修改";            
        }
        else if (Action == "Add")
        {
            btnUpdate.Text = "增加";            
        }
        else if (Action == "Delete")
        {
            btnAction();
            return;
            //btnUpdate.Text = "删除";
        }        

        if (!IsPostBack)
        {
            BindDrop();
            BindTreeDrop("");

            if (Action == "Modify" || Action == "Delete")
            {
                BindData();
            }
        }
    }

    protected void BindData()
    {
        Whir.Model.Whir_Column model = new Whir.SQLServerDAL.Whir_Column().GetModel(ColumnID);
        if (model == null)
            return;

        BindTreeDrop(" and columnID<>" + ColumnID);

        this.ddlModule.SelectedValue = model.moduleMark;
        if (model.moduleMark != "-1" && model.modelID != -1)
        {
            BindModel();
            this.ddlModel.SelectedValue = model.modelID.ToString();

            if (model.formID != "-1")
            {
                if (model.formID.IndexOf('|') != -1)    //表单组
                {
                    divForm.Visible = false;
                    divForms.Visible = true;
                    BindModelForms();
                    this.ddlModelForms.SelectedValue = model.formID;
                }
                else//表单
                {
                    divForms.Visible = false;
                    divForm.Visible = true;
                    BindModelForm();
                    this.ddlForm.SelectedValue = model.formID;
                }
            }
        }

        this.ddlDirectory.SelectedValue = model.directory;

        this.ddlParent.SelectedValue = model.parentID.ToString();
        this.txtColumnName.Text = model.columnName;
        this.txtOperatePage.Text = model.OperatePage;
        this.txtParam.Text = model.param;
        //this.txtSort.Text = model.sort.ToString();
        this.txtUrl.Text = model.url;
        //图片 略
        this.txtMetaTitle.Text = model.metaTitle;
        this.txtMetaKey.Text = model.metaKey;
        this.txtMetaDesc.Text = model.metaDesc;

        this.txtListTemp.Text = model.listTemp;
        this.txtDefaultTemp.Text = model.defaultTemp;
        this.txtContentTemp.Text = model.contentTemp;

        this.cbIsCreateHtml.Checked = model.isCreateHtml;
        this.cbIsUse.Checked = model.isUse;

        this.cbIsUse.Enabled = false;    //权限不可改        
        this.rblIsSinglePage.SelectedIndex = model.IsSinglePage ? 0 : 1;
    }

    /// <summary>
    /// 绑定模块下拉列表
    /// </summary>
    protected void BindDrop()
    {
        //DataTable dt = Whir.SQLServerDAL.DbHelperSQL.GetDataTable("moduleMark,moduleName", "Whir_Module", "", "");
        //this.ddlModule.DataSource = dt.DefaultView;
        //this.ddlModule.DataValueField = "moduleMark";
        //this.ddlModule.DataTextField = "moduleName";
        //this.ddlModule.DataBind();

        //this.ddlModule.Items.Insert(0, new ListItem("-- 模块列表 --", "-1"));
        BindModel();
    }

    /// <summary>
    /// 绑定功能模块下拉列表
    /// </summary>
    protected void BindModel()
    {
        DataTable dt = Whir.SQLServerDAL.DbHelperSQL.GetDataTable("modelID,modelName", "Whir_Model", "", "");
        this.ddlModel.DataSource = dt.DefaultView;
        this.ddlModel.DataValueField = "modelID";
        this.ddlModel.DataTextField = "modelName";
        this.ddlModel.DataBind();

        this.ddlModel.Items.Insert(0, new ListItem("-- 功能模块列表 --", "-1"));
    }

    /// <summary>
    /// 绑定表单下拉列表
    /// </summary>
    protected void BindModelForm()
    {
        DataTable dt = Whir.SQLServerDAL.DbHelperSQL.GetDataTable("modelFormID,formName", "Whir_ModelForm", "modelID=" + this.ddlModel.SelectedValue, "");
        this.ddlForm.DataSource = dt.DefaultView;
        this.ddlForm.DataValueField = "modelFormID";
        this.ddlForm.DataTextField = "formName";
        this.ddlForm.DataBind();
    }

    /// <summary>
    /// 绑定表单组下拉列表
    /// </summary>
    protected void BindModelForms()
    {
        DataTable dt = Whir.SQLServerDAL.DbHelperSQL.GetDataTable("FormsGroup,GroupName", "Whir_ModelForms", "", "");
        this.ddlModelForms.DataSource = dt.DefaultView;
        this.ddlModelForms.DataValueField = "FormsGroup";
        this.ddlModelForms.DataTextField = "GroupName";
        this.ddlModelForms.DataBind();

        this.ddlModelForms.Items.Insert(0, new ListItem("-- 功能模块表单组 --", "-1"));
    }


    //列表事件1
    protected void ddlModule_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.ddlModule.SelectedValue != "-1")
            BindModel();
        else
            this.ddlModel.Items.Clear();

        this.ddlForm.Items.Clear();
        this.ddlModelForms.Items.Clear();
    }

    //列表事件2
    protected void ddlModel_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.ddlModel.SelectedValue != "-1")
        {
            Whir.BLL.Whir_Model bll = new Whir.BLL.Whir_Model();
            bool flag = bll.GetModel(int.Parse(this.ddlModel.SelectedValue)).IsGroup;
            if (flag)
            {
                //divForms.Visible = true;
                //divForm.Visible = false;
                BindModelForms();
            }
            else
            {
                //divForm.Visible = true;
                //divForms.Visible = false;
                BindModelForm();
            }
        }
        else
        {
            this.ddlForm.Items.Clear();
            this.ddlModelForms.Items.Clear();
        }
    }

    /// <summary>
    /// 绑定栏目树型下拉列表
    /// </summary>    
    protected void BindTreeDrop(string str)
    {
        this.ddlParent.DataValueField = "columnID";
        this.ddlParent.DataTextField = "columnName";
        Whir.Common.BindHelper.BindDropDownList(this.ddlParent, "Whir_Column", "0", str, "-- 导航栏目 --");
    }

    /// <summary>
    /// 删除栏目
    /// </summary>
    private void DeleteColumn()
    {
        Whir.SQLServerDAL.Whir_Column bll = new Whir.SQLServerDAL.Whir_Column();
        Whir.Model.Whir_Column model = bll.GetModel(ColumnID);
        if (model != null)
        {
            bll.Delete(ColumnID);
        }

        //删除下级类别
        DeleteByColumnID(ColumnID);

        //修改角色表之栏目id
        RoleHelper.UpdateColumnIDs(ColumnID.ToString());

        //修改角色表之角色值
        //if (this.cbIsUse.Checked)
        //{
        //    int b = Whir.SQLServerDAL.Whir_Column.GetIDSort(ColumnID);
        //    RoleHelper.DeleteColumnRole(b);
        //    RoleHelper.DeleteCacheRole(b);
        //}
    }

    /// <summary>
    /// 修改栏目
    /// </summary>
    private void ModifyColumn()
    {
        Whir.SQLServerDAL.Whir_Column bll = new Whir.SQLServerDAL.Whir_Column();
        Whir.Model.Whir_Column model = bll.GetModel(ColumnID);

        //model.moduleMark = this.ddlModule.SelectedValue;
        //if (divForm.Visible)
        //{
        if (this.ddlForm.Items.Count > 0)
            model.formID = this.ddlForm.SelectedValue;
        else
            model.formID = "-1";
        //}
        if (divForms.Visible)
        {
            if (this.ddlModelForms.Items.Count > 0)
                model.formID = this.ddlModelForms.SelectedValue;
            else
                model.formID = "-1";
        }

        if (this.ddlModel.Items.Count > 0)
        {
            if (ddlModel.SelectedValue != "-1")
            {
                model.modelID = int.Parse(this.ddlModel.SelectedValue);
                Whir.Model.Whir_Model whirModel = new Whir.BLL.Whir_Model().GetModel(int.Parse(ddlModel.SelectedValue));
                if (whirModel != null)
                    model.moduleMark = whirModel.moduleMark;
            }
            else
                model.modelID = -1;
        }
        else
            model.modelID = -1;

        model.parentID = int.Parse(this.ddlParent.SelectedValue);
        model.columnName = this.txtColumnName.Text;
        model.OperatePage = this.txtOperatePage.Text;
        model.param = this.txtParam.Text;
        //model.sort = int.Parse(this.txtSort.Text);
        model.url = this.txtUrl.Text;
        model.pic = "";
        model.metaTitle = this.txtMetaTitle.Text;
        model.metaKey = this.txtMetaKey.Text;
        model.metaDesc = this.txtMetaDesc.Text;
        model.listTemp = this.txtListTemp.Text;
        model.defaultTemp = this.txtDefaultTemp.Text;
        model.contentTemp = this.txtContentTemp.Text;
        model.isCreateHtml = this.cbIsCreateHtml.Checked;

        model.updateDate = DateTime.Now;
        model.updateUserID = RoleHelper.GetAdminID();
        model.directory = this.ddlDirectory.SelectedValue;

        //自动判断是否单篇页
        //if (ddlModule.SelectedValue == "SinglePage_v0.0.01")
        if (model.moduleMark == "SinglePage_v0.0.01")
            model.IsSinglePage = true;
        else
            model.IsSinglePage = false;

        bll.Update(model);
    }

    /// <summary>
    /// 添加栏目编号
    /// </summary>
    /// <returns></returns>
    private int AddColumn()
    {
        string form = this.ddlForm.SelectedValue;
        Whir.Model.Whir_Column model = new Whir.Model.Whir_Column();

        model.columnMark = "Whir_" + DateTime.Now.ToString();
        //model.moduleMark = this.ddlModule.SelectedValue;

        if (this.ddlModel.Items.Count > 0)
        {
            model.modelID = int.Parse(this.ddlModel.SelectedValue);
            Whir.Model.Whir_Model whirModel = new Whir.BLL.Whir_Model().GetModel(int.Parse(ddlModel.SelectedValue));
            if (whirModel != null)
                model.moduleMark = whirModel.moduleMark;
            else
                model.moduleMark = "";
        }
        else
        {
            model.modelID = -1;
            return 0;
        }

        //if (divForm.Visible)
        //{
        if (this.ddlForm.Items.Count > 0)
            model.formID = this.ddlForm.SelectedValue;
        else
            model.formID = "-1";
        //}
        //if (divForms.Visible)
        //{
        //    if (this.ddlModelForms.Items.Count > 0)
        //        model.formID = this.ddlModelForms.SelectedValue;
        //    else
        //        model.formID = "-1";
        //}
        //else
        //    model.formID = "-1";

        model.parentID = int.Parse(this.ddlParent.SelectedValue);
        model.columnName = this.txtColumnName.Text;
        model.OperatePage = this.txtOperatePage.Text;
        model.param = this.txtParam.Text;
        model.sort = GetMaxSort(model.parentID); //int.Parse(this.txtSort.Text);
        model.url = this.txtUrl.Text;
        model.pic = "";
        model.metaTitle = this.txtMetaTitle.Text;
        model.metaKey = this.txtMetaKey.Text;
        model.metaDesc = this.txtMetaDesc.Text;
        model.listTemp = this.txtListTemp.Text;
        model.defaultTemp = this.txtDefaultTemp.Text;
        model.contentTemp = this.txtContentTemp.Text;
        model.isCreateHtml = this.cbIsCreateHtml.Checked;
        model.isUse = this.cbIsUse.Checked;

        model.createDate = DateTime.Now;
        model.createUserID = RoleHelper.GetAdminID();
        model.updateDate = model.createDate;
        model.updateUserID = model.createUserID;
        model.directory = this.ddlDirectory.SelectedValue;

        //自动判断是否单篇页
        //if (ddlModule.SelectedValue == "SinglePage_v0.0.01")
        if (model.moduleMark == "SinglePage_v0.0.01")
            model.IsSinglePage = true;
        else
            model.IsSinglePage = false;
        //model.IsSinglePage = rblIsSinglePage.SelectedIndex == 0 ? true : false;
        int cloumnID = new Whir.SQLServerDAL.Whir_Column().Add(model);

        //同步权限
        if (this.cbIsUse.Checked)
        {
            RoleHelper.AddDefaultRole();
            RoleHelper.AddCacheRole();
        }
        //end

        return cloumnID;
    }

    //按钮事件
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        btnAction();
    }

    /// <summary>
    /// 按钮操作
    /// </summary>
    private void btnAction()
    {
        int columnID = 0;
        string ResultUrl = "";
        switch (Action)
        {
            case "Add":
                columnID = AddColumn();
                ResultUrl = "<script>parent.location='ColumnNewList.aspx?op=" + Action + "&ID=" + columnID + "';</script>";
                break;
            case "Modify":
                ModifyColumn();
                Response.Write("<script>parent.tree.getNodeById(" + ColumnID.ToString() + ").setText('" + this.txtColumnName.Text + "');</script>");
                Response.Write("<script>parent.tree.getNodeById(" + ColumnID.ToString() + ").parentNode.reload();</script>");
                ResultUrl = "<script>parent.location='ColumnNewList.aspx?op=" + Action + "&ID=" + ColumnID + "';</script>";
                break;
            case "Delete":
                DeleteColumn();
                ResultUrl = "<script>location='ColumnNewList.aspx?op=" + Action + "';</script>";
                break;
        }

        Response.Clear();
        Response.Write(ResultUrl);
        Response.End();
        return;
    }


    protected void btnReset_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.Write("<script>parent.document.getElementById('btnAdd').style.display='';</script>");
        Response.End();
        return;
    }


    /// <summary>
    /// 获取该父级类别的最大记录数,已自动+1
    /// </summary>
    /// <param name="ParentId">父级类别编号</param>
    private int GetMaxSort(int ParentId)
    {
        string strSQL = String.Format("SELECT COUNT(1) + 1 FROM Whir_Column WHERE parentID = {0}", ParentId);
        object obj = Whir.SQLServerDAL.DbHelperSQL.GetSingle(strSQL);

        return obj != null ? Convert.ToInt32(obj) : 0;
    }

    /// <summary>
    /// 删除所以子类
    /// </summary>
    private void DeleteByColumnID(int columnID)
    {
        string ids = GetChildID(columnID.ToString());

        if (ids != string.Empty)
        {
            string strSQL = String.Format("DELETE FROM Whir_Column WHERE columnID IN ({0})", ids.TrimStart(',').TrimEnd(','));
            Whir.SQLServerDAL.DbHelperSQL.ExecuteSql(strSQL);
        }
    }

    /// <summary>
    /// 获取所以子类编号
    /// </summary>
    private string GetChildID(string parentID)
    {
        string strResult = "";
        if (parentID == "") return strResult;

        string strSQL = String.Format("SELECT columnID FROM Whir_Column WHERE parentID = {0}", parentID);
        DataTable dt = Whir.SQLServerDAL.DbHelperSQL.Query(strSQL).Tables[0];

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            strResult += GetChildID(dt.Rows[i]["columnID"].ToString()) + ",";
            strResult += dt.Rows[i]["columnID"].ToString();
        }

        return strResult;
    }
}
